<?php

class default_DonhangController extends ZF_DefaultController {

    public function indexAction() {
        $model_daily = new Default_Model_Daily();
        
        $this->view->header = "Nhập đơn hàng";
//        $this->view->daily = $model_daily->listAll();
        

        if ($this->getRequest()->isPost()) {
//            $dl_ban = $this->getRequest()->getParam('dl_ban');
//            $dl_mua = $this->getRequest()->getParam('dl_mua');
            $dl_ban = 1;
            $dl_mua = 1;            
            
            $model_sanpham = new Default_Model_Sanpham();
            $model_daily = new Default_Model_Daily();
            $model_trackdh = new Default_Model_Trackdonhang();
            $data = $model_sanpham->listAll();
            
            $dl_gioithieu = $this->getRequest()->getParam('dl_gioithieu');
            $nguoidaidien = $model_daily->getById($dl_gioithieu);
            if($nguoidaidien && $nguoidaidien != -1){
                $dl_gioithieu = $nguoidaidien['MaDaiLyCha'];
            }
            
            if ($data) {
                $tongtien = 0;
                $dsCTDH = "";
                $model_chitietdonhang = new Default_Model_Chitietdonhang();
                
                foreach ($data as $item) {
                    $masanpham = $item['MaSanPham'];
                    $quantity = (int)$this->getRequest()->getParam('sl' . $masanpham);
                    $thue = (int)$this->getRequest()->getParam('th' . $masanpham);
                    if ($quantity && $quantity > 0) {
                        $temp = $quantity * (int)$item['DonGia'];
                        $temp += $temp * ($thue / 100);
                        $ctdh = $model_chitietdonhang->insert(" ", $masanpham, $quantity, $thue, $temp);
                        if ($ctdh) {
                            $dsCTDH .= $ctdh . ",";
                        }
                        $tongtien += $temp;
                    }
                }
                
                if ($dsCTDH != "") {
                    $dsCTDH = substr($dsCTDH, 0, strlen($dsCTDH) - 1);
                }
                
                if ($tongtien > 0) {
                    $model_donhang = new Default_Model_Donhang();
                    $ngayban = $this->getRequest()->getParam('ngayban');
                    $arrTemp = explode(" ", $ngayban);
                    $date = explode("/", $arrTemp[0]);
                    $madonhang = $model_donhang->insert($dl_ban, $tongtien, $ngayban, $dl_mua, $dl_gioithieu, $dsCTDH, 0, 0);
                    if ($madonhang != -1) {
                        
                        $cur_daily = $model_daily->getById($dl_gioithieu);
                        $maLoaiDaiLy = $cur_daily['MaLoaiDaiLy'];
                        
                        $model_loaidaily = new Default_Model_Loaidaily();
                        $cur_loaidaily = $model_loaidaily->getById($maLoaiDaiLy);
                        
                        $loinhuan = $tongtien * ($cur_loaidaily['PhanTramHoaHong'] / 100);
                        $bonus = $tongtien * ($cur_loaidaily['PhanTramBonus'] / 100);
                        
                        $matrack = $dl_gioithieu."_".$date[1]."_".$date[2];
                        $cur_trackDetail = $model_trackdh->getById($matrack);
                        if (!$cur_trackDetail) {
                            $model_trackdh->insert($matrack, $madonhang, "", $loinhuan, $bonus);
                        } else {
                            $cur_trackDetail['LoiNhuan'] += $loinhuan;
                            $cur_trackDetail['Bonus'] += $bonus;
                            $cur_trackDetail['DSMaDHTuBan'] .= "," . $madonhang;
                            $model_trackdh->update($matrack, $cur_trackDetail);
                        }
                        
                        $madailycha = $cur_daily['MaDaiLyCha'];
                        while($madailycha > 0) {
                            $parent_daily = $model_daily->getById($madailycha);
                            
                            $maLoaiDaiLy = $parent_daily['MaLoaiDaiLy'];
                            $cur_loaidaily = $model_loaidaily->getById($maLoaiDaiLy);
                            $bonus = $tongtien * ($cur_loaidaily['PhanTramBonus'] / 100);
                            
                            $matrack = $madailycha."_".$date[1]."_".$date[2];
                            $cur_trackDetail = $model_trackdh->getById($matrack);
                            if (!$cur_trackDetail) {
                                $model_trackdh->insert($matrack, "", $madonhang, 0, $bonus);
                            } else {
                                $cur_trackDetail['Bonus'] += $bonus;
                                $cur_trackDetail['DSMaDHCapCon'] .= "," . $madonhang;
                                $model_trackdh->update($matrack, $cur_trackDetail);
                            }
                            
                            $madailycha = $parent_daily['MaDaiLyCha'];
                        }
                        
                        $dsCTDH = explode(',', $dsCTDH);
                        for ($i = 0; $i < count($dsCTDH); $i++) {
                            $mactdh = $dsCTDH[$i];
                            $data = $model_chitietdonhang->getById($mactdh);
                            $data['MaDonHang'] = $madonhang;
                            $model_chitietdonhang->update($mactdh, $data);
                        }
                        $this->view->success = true;
                    }
                }
            }
        }
    }

    public function getsanphamAction() {
        $model_sanpham = new Default_Model_Sanpham();
        $data = $model_sanpham->listAll();
        if ($data) {
            $jsonObject = array('ErrorMessage' => '1', 'result' => $data);
            $this->_helper->json->sendJson($jsonObject);
        } else {
            $jsonObject = array('ErrorMessage' => '-1');
            $this->_helper->json->sendJson($jsonObject);
        }
    }
}
